#!/bin/bash

set -e
token = "6966be373c70173282db692b3a694fcc"
CURRENT_DIR=$(cd $(dirname $0); pwd)
function install-build-env(){
    dnf install gcc -y
    dnf install gcc-c++ -y
    dnf install m4 -y
    dnf install flex -y
    dnf install bison -y
    dnf install elfutils-libelf-devel -y
    dnf install openssl-devel -y
    dnf install dwarves -y
}
function build-linux(){
    make -C linux ARCH=x86_64 defconfig
    make -C linux bzImage
}

function build-busybox(){
    cp ./busybox-config busybox/.config
    make -C busybox
}

function build-rootfs(){
    dd if=/dev/zero of=rootfs.img bs=1M count=4096
    mkfs.ext4 rootfs.img
    mkdir rootfs
    mount -t ext4 -o rw rootfs.img ./rootfs
    make -C busybox install CONFIG_PREFIX=../rootfs
    mkdir rootfs/etc rootfs/dev rootfs/home rootfs/root rootfs/proc rootfs/sys rootfs/mnt
    umount rootfs
}
function os-start(){
    qemu-kvm -kernel ./linux/arch/x86_64/boot/bzImage -hda ./rootfs.img -append "root=/dev/sda console=ttyS0" -nographic
}
install-build-env
build-linux
build-busybox
build-rootfs
os-start
